package HCI.src;
import java.sql.Statement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;


public class DBconnector {
int size_ofrows=0;
	

	public static Connection connect() 
	{
		Connection connection = null; 
		try { 
		// Load the JDBC driver 
		String driverName = "com.mysql.jdbc.Driver"; // MySQL MM JDBC driver 
		Class.forName(driverName); 
		// Create a connection to the database 
		String serverName = "localhost"; 
		String mydatabase = "cookdb"; 
		String url = "jdbc:mysql://" + serverName + "/" + mydatabase; // a JDBC url 
		String username = "root"; 
		String password = "root"; 
		connection = DriverManager.getConnection(url, username, password); 
		return connection;
		} catch (ClassNotFoundException e) { 
			System.out.println("connection error");
			return null;
		// Could not find the database driver 
		} catch (SQLException e) { 
			System.out.println("sql error");
			return null;
		// Could not connect to the database 
		}
	}
	
	public ResultSet excuteQuery(String Querry ,Connection c)
	{
		ResultSet rs = null ;
		ResultSet temp = null ;
		try{
			
			Statement stmt = (Statement) c.createStatement();
			Statement tempst = (Statement) c.createStatement();

			 rs = ((java.sql.Statement) stmt).executeQuery(Querry);
			temp=((java.sql.Statement) tempst).executeQuery(Querry);
			//temp = (ResultSet) rs.getMetaData();
				if(rs.isBeforeFirst()){
					System.out.println("ana if f el db connnector");
					while(temp.next()){
						
						size_ofrows++;
					}
				return rs;
				}
				else{
					System.out.println("ana else f el db connnector");
					rs=null;
					return rs;
				}
			
		} 
		catch(Exception e)
		{
			System.out.println("lol");
		}
		return rs;
	}
	
	public  boolean UpdateQuery(String Querry ,Connection c)
	{
		int rs = 0 ;
		try{
			System.out.println("hello from 80");
			Statement stmt = (Statement) c.createStatement();

			 rs =  stmt.executeUpdate(Querry);
			//temp = (ResultSet) rs.getMetaData();
				if(rs>0){
					System.out.println("ana if f el db connnector");
					
				return true;
				}
				else{
					System.out.println("ana else f el db connnector");
					
					return false;
				}
			
		} 
		catch(Exception e)
		{
			System.out.println("lolzz");
		}
		return false;
	}
}
